package com.bloody.android.Map;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Random;

import com.bloody.android.BloodyCrusadeGame;
import com.bloody.android.ViewObject;
import com.bloody.android.Characters.Character;

import android.content.Context;
import android.util.Log;

public class Map implements Serializable{
	
	private Case[][] cases;
	private int row;
	private int column;
	
	public Map(int row, int column){
		this.row = row;
		this.column = column;
		init();
	}
	
	public Map(String[] datas)
	{
		row = Integer.parseInt(datas[0]);
		column = Integer.parseInt(datas[1]);
	
		cases = new Case [row][column];
		
		for (int i=0; i<row; i++){
			for (int j=0; j<column; j++)
				cases[i][j] = CaseFactory.getInstance(Integer.parseInt(datas[(i*column)+j+2]), i, j);
		}
	}
	
	public Case[][] getCases() {		
		return cases;
	}

	public String toString(){
		String map="";
		
		map+=new String(Integer.toString(this.row)
				+ " " + Integer.toString(this.column) + " ");
		for(int i=0 ; i<this.row ; i++)
		{
			for(int j=0 ; j<this.column ; j++)
			{
				map+=Integer.toString(this.cases[i][j].getLandscape()) + " ";
			}
		}
		
		return map.trim();
	}

	private void init(){
		Random r = new Random();
		cases = new Case [row][column];
		for (int i=0; i<row; i++){
			for (int j=0; j<column; j++){
				int alea = r.nextInt(3)+1;
				cases[i][j] = CaseFactory.getInstance(alea, i, j);
			}
		}
	}

	public int getRow() {
		return row;
	}
	
	public void removeCharacter(int i, int j){
		cases[i][j].removeCharacter();
	}
	
	public void addCharacter(int i, int j, Character c){
		cases[i][j].addCharacter(c);
	}
	
	public boolean isItSomeone(int i, int j){
		if (i>=0 && i<row && j>=0 && j<column)
			return cases[i][j].isItSomeone();
		else
			return false;
	}
	
	public void setCharacter(int i, int j, Character c){
		cases[i][j].addCharacter(c);
		c.setPosition(j, i);
		Log.d("DEBUG", "characterMap");
		c.setMap(this);
	}

	public int getColumn() {
		return column;
	}
	
	public int getCase(int i, int j){
		return cases[i][j].getLandscape();
	}
	
	public void display(int i, int j, BloodyCrusadeGame bc){
		cases[i][j].display(bc);
	}
	
	public void hidden(int i, int j){
		cases[i][j].hidden();
	}
	
	public void unCheck(){
		for (int i=0; i<row; i++){
			for (int j=0; j<column; j++){
				cases[i][j].setCheck(false);
			}
		}
	}
	
	public Character getCharacter(int posx, int posy){
		return cases[posy][posx].getCharacter();		
	}
	
	public boolean isItOneCheck(){
		for (int i=0; i<row; i++){
			for (int j=0; j<column; j++){
				if (cases[i][j].isCheck())
					return true;
			}
		}
		return false;
	}
	
	public boolean isCheck(int posx, int posy){
		return cases[posy][posx].isCheck();
	}
	
	private void checkCaseMove(int posx, int posy, int move){
		if (posx>=0 && posx<column && posy>=0 && posy<row){
			if (!isItSomeone(posy, posx)){
				cases[posy][posx].setCheck(true);
				if (move>0){
					move-=1;
					checkCaseMove(posx,posy+1,move);
					checkCaseMove(posx+1,posy,move);
					checkCaseMove(posx,posy-1,move);
					checkCaseMove(posx-1,posy,move);
				}	
			}
		}
	}
	
	public void setViewCase(Context context, ArrayList<ViewObject> vElement){
		for (int i=0; i<row; i++){
			for (int j=0; j<column; j++){
				cases[i][j].setViewCase(context, vElement);
			}
		}
	}
	
	public void setCaseCheck(int posx, int posy){
		cases[posy][posx].setCheck(true);
	}
	
	public void showMove(int posx, int posy, int move){
		move-=1;
		checkCaseMove(posx,posy+1,move);
		checkCaseMove(posx+1,posy,move);
		checkCaseMove(posx,posy-1,move);
		checkCaseMove(posx-1,posy,move);
	}
	
	public void showRange(int[] attackRange, int posx, int posy, int player){
		for (int i=0; i<attackRange.length; i++){
			for (int x=-(attackRange[i]); x<=attackRange[i]; x++){
				for (int y=-(attackRange[i]); y<=attackRange[i]; y++){
					if (Math.abs(x)+Math.abs(y)==attackRange[i]){
						if (isItSomeone(posy+y, posx+x)){
							if (getCharacter(posx+x, posy+y).getPlayer()!=player){
								setCaseCheck(posx+x, posy+y);
							}
						}
					}
				}
			}
		}
		
	}
}
